﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Project.Co_DataTransformationPrototype
{
    public class MoveMapping : MappingBase
    {
        Entity sourceEntity;

        public MoveMapping(EventContext eventContext, Entity sourceEntity)
        {
            this.eventContext = eventContext;
            this.sourceEntity = sourceEntity;
            //generateSQLCodeMapping();
        }

        internal override void generateSQLCodeMapping(StringBuilder existingScript = null)
        {
            strSQLMapping = new StringBuilder();
            
            if (existingScript != null)
                strSQLMapping.Append(existingScript);

            setMoveSQLCode();
        }

        private void setMoveSQLCode()
        {
            Entity targetEntity;
            EntityAttribute attribute;
            IList<string> idAttributes;
            string strTargetEntityName;

            attribute = eventContext.elementEventArgs.ModelElement as EntityAttribute;
            targetEntity = attribute.Entity;
            
            if (targetEntity == null)
                targetEntity = attribute.ParentEntity;

            if (sourceEntity != null)
            {
                strTargetEntityName = targetEntity.CurrentName;
                idAttributes = MappingJoinCondition.findMatchingIDAttributes((sourceEntity as Entity), targetEntity);

                // Introduce new column in target table
                strSQLMapping.AppendFormat(@"/*** --- Moving column {0} from source table {1} to target table {2} --- ***/" + Environment.NewLine, attribute.CurrentName, sourceEntity.CurrentName, strTargetEntityName);
                strSQLMapping.AppendFormat(@"ALTER TABLE [{0}].{1} ADD {2} {3}" + Environment.NewLine, Data.DatabaseSettings.SHEMA_NAME, strTargetEntityName, attribute.CurrentName, MappingTypeConvertion.translateDataTypeIntoSQLString(attribute.Type));
                strSQLMapping.AppendLine("GO" + Environment.NewLine);

                // Move column data from source to target table
                if (idAttributes.Count == 2)
                {
                    strSQLMapping.AppendFormat(@"UPDATE [{0}].{1} SET {2} = " + Environment.NewLine, Data.DatabaseSettings.SHEMA_NAME, strTargetEntityName, attribute.CurrentName);
                    strSQLMapping.AppendFormat(@"    (SELECT {0} FROM {1}" + Environment.NewLine, attribute.CurrentName, sourceEntity.CurrentName);
                    strSQLMapping.AppendFormat(@"    WHERE {0} = [{1}].{2}" + Environment.NewLine, idAttributes[0], strTargetEntityName, idAttributes[1]);
                    strSQLMapping.AppendLine("    )");
                }
                else
                {
                    strSQLMapping.AppendFormat(@"INSERT INTO [{0}].{1} ({2})" + Environment.NewLine, Data.DatabaseSettings.SHEMA_NAME, strTargetEntityName, attribute.CurrentName);
                    strSQLMapping.AppendFormat(@"    SELECT {0}" + Environment.NewLine, attribute.CurrentName);
                    strSQLMapping.AppendFormat(@"    FROM [{0}].{1}" + Environment.NewLine, Data.DatabaseSettings.SHEMA_NAME, sourceEntity.CurrentName);
                }
                strSQLMapping.AppendLine("GO" + Environment.NewLine);

                // Removal of column from source table will be done by other event
            }
            else
            {
                throw new NotImplementedException("setMoveSQLCode: scenario -> target entity not defined yet. Call the monkeys!");
            }
        }
    }
}
